package com.hss01248.dialog.ios;

import com.hss01248.dialog.ResourceTable;
import com.hss01248.dialog.Tool;
import com.hss01248.dialog.adapter.SuperLvHolder;
import com.hss01248.dialog.config.ConfigBean;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.components.element.StateElement;
import ohos.app.Context;
import ohos.agp.components.Text.TextSizeType;

/**
 * Created by Administrator on 2016/10/9 0009.
 */
public class IosActionSheetHolder extends SuperLvHolder<ConfigBean> {
    public ListContainer lv;
    protected Button btnBottom;
    public Text textView;
    private Component line;

    public IosActionSheetHolder(Context context) {
        super(context);
    }

    @Override
    protected void findViews() {
        lv = (ListContainer) rootView.findComponentById(ResourceTable.Id_lv);
        textView = (Text)rootView.findComponentById(ResourceTable.Id_tv_title);
        line = rootView.findComponentById(ResourceTable.Id_v_line);
        btnBottom = (Button) rootView.findComponentById(ResourceTable.Id_btn_bottom);
    }

    @Override
    protected int setLayoutRes() {
        return ResourceTable.Layout_dialog_ios_alert_bottom;
    }

    @Override
    public void assingDatasAndEvents(final Context context, final ConfigBean bean) {

        if(Tool.isEmpty(bean.title)){
            textView.setVisibility(Component.HIDE);
            line.setVisibility(Component.HIDE);
        }else {
            textView.setVisibility(Component.VISIBLE);
            line.setVisibility(Component.VISIBLE);
            textView.setText(bean.title.toString());
            if(bean.titleTxtSize>0){
                textView.setTextSize(bean.titleTxtSize, TextSizeType.FP);
            }
            if(bean.titleTxtColor !=0){
                Tool.setTextColorById(textView, bean.titleTxtColor);
            }
        }


        if (Tool.isEmpty(bean.bottomTxt)){
            btnBottom.setVisibility(Component.HIDE);
        }else {
            btnBottom.setVisibility(Component.VISIBLE);
            btnBottom.setText(bean.bottomTxt.toString());
            StateElement se = new StateElement();
            se.addState(new int[]{ComponentState.COMPONENT_STATE_PRESSED}, getAllCorner(context, ResourceTable.Graphic_bg_ios_no_roundcorner_gray));
            se.addState(new int[]{ComponentState.COMPONENT_STATE_EMPTY}, getAllCorner(context, ResourceTable.Graphic_bg_ios_no_roundcorner_white));
            btnBottom.setBackground(se);
            btnBottom.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component v) {
                    Tool.dismiss(bean);
                    bean.itemListener.onBottomBtnClick();

                }
            });
        }


        BaseItemProvider adapter = new BaseItemProvider() {
            @Override
            public int getCount() {
                return bean.wordsIos.size();
            }

            @Override
            public Object getItem(int position) {
                return bean.wordsIos.get(position);
            }

            @Override
            public long getItemId(int position) {
                return 0;
            }

            @Override
            public Component getComponent(final int position, Component convertView, ComponentContainer parent) {
                LayoutScatter scatter = LayoutScatter.getInstance(context);

                DependentLayout root = (DependentLayout)scatter.parse(ResourceTable.Layout_item_btn_bottomalert, null, false);
                Button view = (Button) root.findComponentById(ResourceTable.Id_btn);
                Component line = root.findComponentById(ResourceTable.Id_btn_line);
                StateElement se = new StateElement();
                if (getCount() >=2) {
                    if (position ==0){
                        if(Tool.isEmpty(bean.title)){
                            se.addState(new int[]{ComponentState.COMPONENT_STATE_PRESSED}, getTopCorner(context, ResourceTable.Graphic_bg_ios_no_roundcorner_gray));
                            se.addState(new int[]{ComponentState.COMPONENT_STATE_EMPTY}, getTopCorner(context, ResourceTable.Graphic_bg_ios_no_roundcorner_white));
                        }else {
                            se.addState(new int[]{ComponentState.COMPONENT_STATE_PRESSED}, getNoCorner(context, ResourceTable.Graphic_bg_ios_no_roundcorner_gray));
                            se.addState(new int[]{ComponentState.COMPONENT_STATE_EMPTY}, getNoCorner(context, ResourceTable.Graphic_bg_ios_no_roundcorner_white));
                        }
                    } else if (position == getCount() -1){
                        se.addState(new int[]{ComponentState.COMPONENT_STATE_PRESSED}, getButtomCorner(context, ResourceTable.Graphic_bg_ios_no_roundcorner_gray));
                        se.addState(new int[]{ComponentState.COMPONENT_STATE_EMPTY}, getButtomCorner(context, ResourceTable.Graphic_bg_ios_no_roundcorner_white));
                        line.setVisibility(Component.HIDE);
                    } else {
                        se.addState(new int[]{ComponentState.COMPONENT_STATE_PRESSED}, getNoCorner(context, ResourceTable.Graphic_bg_ios_no_roundcorner_gray));
                        se.addState(new int[]{ComponentState.COMPONENT_STATE_EMPTY}, getNoCorner(context, ResourceTable.Graphic_bg_ios_no_roundcorner_white));
                    }
                } else {
                    se.addState(new int[]{ComponentState.COMPONENT_STATE_PRESSED}, getAllCorner(context, ResourceTable.Graphic_bg_ios_no_roundcorner_gray));
                    se.addState(new int[]{ComponentState.COMPONENT_STATE_EMPTY}, getAllCorner(context, ResourceTable.Graphic_bg_ios_no_roundcorner_white));
                    line.setVisibility(Component.HIDE);
                }

                view.setBackground(se);
                view.setText(bean.wordsIos.get(position).toString());
                view.setClickedListener(new Component.ClickedListener() {
                    @Override
                    public void onClick(Component v) {
                        Tool.dismiss(bean,true);
                        bean.itemListener.onItemClick(bean.wordsIos.get(position),position);

                    }
                });

                return root;
            }
        };

        lv.setItemProvider(adapter);
    }
    private ShapeElement getNoCorner(Context context, int xmlId)
    {
        ShapeElement se = new ShapeElement(context, xmlId);
        return se;
    }
    private ShapeElement getAllCorner(Context context, int xmlId)
    {
        ShapeElement se = new ShapeElement(context, xmlId);
        se.setCornerRadiiArray(new float[]{20,20,20,20,20,20,20,20});
        return se;
    }
    private ShapeElement getTopCorner(Context context, int xmlId)
    {
        ShapeElement se = new ShapeElement(context, xmlId);
        se.setCornerRadiiArray(new float[]{20,20,20,20,0,0,0,0});
        return se;
    }
    private ShapeElement getButtomCorner(Context context, int xmlId)
    {
        ShapeElement se = new ShapeElement(context, xmlId);
        se.setCornerRadiiArray(new float[]{0,0,0,0,20,20,20,20});
        return se;
    }
}
